package controller;

import com.alibaba.fastjson.JSON;
import entity.Blog;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;
import service.UserServiceImpl;
import utils.z_duanxin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
 @Autowired
    public UserService service= new UserServiceImpl();


    @RequestMapping("/login")
    public String login(@RequestParam(value = "login-email",defaultValue = "")String uphone, @RequestParam(value = "login-password",defaultValue = "")String upwd, Model model, HttpSession session){
        User user= service.userlogin(uphone);

       if(user ==null){
           model.addAttribute("err","1;");//报错了
           return "redirect:/Directory/index";
       }
       else if(!user.getUpwd().equals(upwd)){
           model.addAttribute("err","2;");//密码错误了
           return "redirect:/Directory/index";
        }


       else if(user.getGrade()==1){
           //管理员账号
           session.setAttribute("sos",user);
           return "redirect:/Directory/hou";
       }


       else if(user.getGrade()!=1){
           model.addAttribute("err","0");//登录成功
           session.setAttribute("sos",user);
           return "redirect:/Blog/FilrendBlog";
       }
        return "redirect:/Directory/index";

    }

    @RequestMapping("insert")
    public  String insert(String fname,String lname, String uaddress,String password,String email,int gender, Model model){

        User u=new User();
        u.setUname(fname);
        u.setUsex(gender);
        u.setUaddress(uaddress);
        u.setUpwd(password);
        u.setUemail(email);
        u.setUphone(lname);
        int count=service.insert(u);
        if(count==1){
            model.addAttribute("err","0");//注册成功
        }
        else{
            model.addAttribute("err","1");//注册失败

        }
        return "redirect:/Directory/login";
    }



    @RequestMapping("duanxin")
    @ResponseBody
    public  String  duanxin(String uphone,HttpSession session){

        z_duanxin duan=new z_duanxin();
        int random = (int) ((Math.random() * 9 + 1) * 1000);
        duan.duanxin(uphone,random);
        session.setAttribute("duan",random);
        String cjson= JSON.toJSONString(random);
        return cjson;
    }

    @RequestMapping("gerenxian")
    public String gerenxian(){

        return "user-geren";
    }

    @RequestMapping("xiugai")
    public String xiugai(String uaddress,String lname,String password,String fname,int gender,int id,String email,Model model){
        User u=new User();
        u.setUaddress(uaddress);
        u.setUname(fname);
        u.setUphone(lname);
        u.setUpwd(password);
        u.setUsex(gender);
        u.setUid(id);
        u.setUemail(email);
       int count= service.updateByPrimaryKey(u);

       if(count==1){//修改成功
            model.addAttribute("err",1);
           return "redirect:/Directory/user-timeline";

       }
       else {//修改失败
           model.addAttribute("err",0);
           return "redirect:/Directory/index";
       }
    }

    @RequestMapping("/Right")
    @ResponseBody
    public Object Right(Model model, HttpSession session, HttpServletRequest request) {
        User user2=(User)session.getAttribute("sos");
        List<User> list=service.getRight(user2.getUid());//显示自己右侧头像
        String cjson= JSON.toJSONString(list);
        return cjson;
    }


}
